//
//  MeetingRoomGateway.swift
//  GOFM
//
//  Created by siri on 2024/10/22.
//  Copyright © 2024 Glad. All rights reserved.
//


import Combine
import Foundation
import Factory


// 定义网关协议
protocol MyMeetingRoomGatewayProtocol {
    func getMyMeetingRoom() -> AnyPublisher<[MyMeetingRoom], Error>

}

// 定义网关实现类
struct MyMeetingRoomGateway:MyMeetingRoomGatewayProtocol {
    func getMyMeetingRoom() -> AnyPublisher<[MyMeetingRoom], Error> {
        Future<[MyMeetingRoom], Error> { promise in
            let meetings = [
                MyMeetingRoom(name: "iPhone", price: 999),
                MyMeetingRoom(name: "MacBook", price: 2999)
            ]
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                promise(.success(meetings))
            }
        }
        .eraseToAnyPublisher()
    }
}

struct PreviewMyMeetingRoomGateway:MyMeetingRoomGatewayProtocol {
    func getMyMeetingRoom() -> AnyPublisher<[MyMeetingRoom], Error> {
        Future<[MyMeetingRoom], Error> { promise in
            let meetings = [
                MyMeetingRoom(name: "iPhone", price: 999),
                MyMeetingRoom(name: "MacBook", price: 2999)
            ]
            
            promise(.success(meetings))
        }
        .eraseToAnyPublisher()
    }
}

extension Container {
    var myMeetingRoomGateway: Factory<MyMeetingRoomGatewayProtocol> {
        Factory(self) { MyMeetingRoomGateway() }
            .onPreview {
                PreviewMyMeetingRoomGateway()
            }
    }
}
